package thread.createThread;

import java.util.concurrent.TimeUnit;

//  线程之两阶段终止模式

public class InterruptedThread {
  public static void main(String[] args) throws InterruptedException {
    Monitor monitor = new Monitor();
    monitor.start();
    Thread.sleep(5000);
    monitor.stop();
  }

  public static class Monitor{
    Thread monitor;

    public void start(){
      monitor = new Thread(()->{
        while (true){
          if (Thread.currentThread().isInterrupted()){
            System.out.println("线程打断了，我歇逼了");
            break;
          }
          System.out.println("我在执行监控任务，美滋滋");
          try {
            Thread.sleep(1000);
          } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
          }
        }
      });

      monitor.start();
    }

    public void stop(){
      monitor.interrupt();
    }
  }
}
